package _thread_lock._futurepattern._future2;

/**
 * @Author: huangya
 * @Date: 2020/10/27
 * @Description:
 */
public class FutureTask<V> implements Future<V> {

    private Object LOCK = new Object();

    private boolean isDone = false;

    private V result;

    @Override
    public boolean isDone() {
        return isDone;
    }

    @Override
    public V get() {
        synchronized (LOCK) {
            while (!isDone) {
                try {
                    LOCK.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public void finish(V input){
        result = input;
        isDone = true;
        synchronized (LOCK) {
            LOCK.notifyAll();
        }
    }
}
